package com.example.API;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Date2Class {
    public static void main(String[] args) throws ParseException {
        //当前时间的Date对象
        Date date = new Date();

        //创建一个日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = sdf.format(date);
        System.out.println(format); //2024年08月18日 12:26:40

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format = sdf.format(date);
        System.out.println(format); //2024-08-18 12:26:40

        sdf = new SimpleDateFormat("y M d");
        format = sdf.format(date);
        System.out.println(format); //2024 8 18


        String s = "2020-2-22 11:11:11";

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf1.parse(s);
        System.out.println(parse);  //Sat Feb 22 11:11:11 CST 2020

    }
}
